//
//  MusicRequest.m
//  One
//
//  Created by mac on 16/5/26.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "MusicRequest.h"

@implementation MusicRequest

+ (void)musicHomePageRequest
{
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSString *urlStr = [NSString stringWithFormat:@"%@idlist/0",BASE_MUSIC_URL];
    [manager GET:urlStr parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (responseObject) {
            NSArray *listArray = responseObject[@"data"];
            [NOTI_CENTER postNotificationName:@"musicHomePageData" object:listArray];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}

+ (void)musicBackListWithMonthDay:(NSString *)monthDay withBlock:(void (^)(NSArray *))Block
{
     NSString *urlStr = [NSString stringWithFormat:@"%@%@",BASE_MUSIC_MONTH,monthDay];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:urlStr parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSArray*arr = responseObject[@"data"];
        NSArray *dataArr = [MusicByMonth mj_objectArrayWithKeyValuesArray:arr];
        NSLog(@"%@",dataArr);
        Block(dataArr);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }
     ];
    
}

+ (void)musicDetailRequestWithMusicId:(int)musicId withBlock:(void (^)(MusicModleObject *))Block
{
    NSString *urlStr = [NSString stringWithFormat:@"%@detail/%d",BASE_MUSIC_URL,musicId];
    
    AFHTTPSessionManager *manmager = [AFHTTPSessionManager manager];
    [manmager GET:urlStr parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (responseObject) {
            NSDictionary *dic = responseObject[@"data"];
            
            MusicModleObject *musicObj = [MusicModleObject mj_objectWithKeyValues:dic];
            Block(musicObj);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];

}

+ (void)musicCommentsWithMusicId:(int)musicId LastCommentId:(int)lastCommentId withBlock:(void (^)(musicCommentsModelObject *, NSArray *, NSArray *))Block
{
    AFHTTPSessionManager *manger = [AFHTTPSessionManager manager];
    NSString *urlStr = [NSString stringWithFormat:@"%@%d/%d",BASE_COMMENT_URL,musicId,lastCommentId];
    
    [manger GET:urlStr parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        musicCommentsModelObject *micObject = [musicCommentsModelObject mj_objectWithKeyValues:responseObject[@"data"]];
        NSArray *dataArray = micObject.data;
        NSMutableArray *hotCommentsArr = [[NSMutableArray alloc]init];
        NSMutableArray *commentArr = [[NSMutableArray alloc]init];
        [dataArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            musicCommentsObject *commObj = obj;
            if (commObj.type == 0) {
                [hotCommentsArr addObject:commObj];
            }
            else
            {
                [commentArr addObject:commObj];
            }
            
        }];
        Block(micObject,hotCommentsArr,commentArr);
    
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}

@end
